#include "FlamethrowerGun.h"
#include "ObjectManager.h"
#include "SGD Wrappers\CSGD_TextureManager.h"
#include "SGD Wrappers/CSGD_XAudio2.h"
#include "Player.h"
#include "CreateProjectileMessage.h"
#include "MessageSystem.h"
#include "GamePlayState.h"

FlamethrowerGun::FlamethrowerGun(Player* owner)
{
	SetWidth(14);
	SetHeight(30);
	SetTextureID(CSGD_TextureManager::GetInstance()->LoadTexture(_T("Graphics/FlameThrower.png")));

	SetMetalPrice(75);
	SetCircuitPrice(75);

	player = owner;
	SetX(owner->GetX() + owner->GetWidth());
	SetY(owner->GetY());

	ObjectManager::GetInstance()->AddObject(this);
	Release();

	type = OBJ_WEAPON;
	rangedType = RW_FLAME;
	attackReady = false;
	specialReady = false;
	ROF = 0.05f;
	shotTimer = ROF;
	cooldownSeconds = 10.0f;
	cooldownTimer = cooldownSeconds;
	energyCost = 15;
	priceScrap = 75;
	priceCircuit = 75;

	//flameShot = CSGD_XAudio2::GetInstance()->SFXLoadSound(_T("Sound/Ranged Weapons/flameShot.wav"));

	emitter  = Emitter::LoadEmitter("Emitters/flameShot.xml");
}

FlamethrowerGun::~FlamethrowerGun()
{
	if(GetTextureID() != -1 )
	{
		CSGD_TextureManager::GetInstance()->UnloadTexture(GetTextureID());
		SetTextureID(-1);
	}
	//if(flameShot != -1)
	//{
	//	CSGD_XAudio2::GetInstance()->SFXStopSound(flameShot);
	//	CSGD_XAudio2::GetInstance()->SFXUnloadSound(flameShot);
	//	flameShot = -1;
	//}

	emitter->Shutdown();


	ObjectManager::GetInstance()->RemoveObject(this);
}

bool FlamethrowerGun::CheckCollision(IEntity* base)
{
	return false;
}

void FlamethrowerGun::Attack()
{
	if(GetOwner()->GetClock() == false)
	{
		CreateProjectileMessage* msg = new CreateProjectileMessage(FLAME, this);
		MessageSystem::GetInstance()->SendMsg(msg);
		msg = nullptr;
		//CSGD_XAudio2::GetInstance()->SFXPlaySound(flameShot);
		emitter->Burst();
	}
	else if(GetOwner()->GetClock() == true)
	{
		CreateProjectileMessage* msg = new CreateProjectileMessage(FLAME_CLOCKED, this);
		MessageSystem::GetInstance()->SendMsg(msg);
		msg = nullptr;
		//CSGD_XAudio2::GetInstance()->SFXPlaySound(flameShot);
		emitter->Burst();
	}

	attackReady = false;
	shotTimer = 0;
}

void FlamethrowerGun::SpecialAttack()
{
	if(GetOwner()->GetClock() == false)
	{
		CreateProjectileMessage* msg = new CreateProjectileMessage(NAPALM, this);
		MessageSystem::GetInstance()->SendMsg(msg);
		msg = nullptr;
		emitter->Burst();
	}
	else if(GetOwner()->GetClock() == true)
	{
		CreateProjectileMessage* msg = new CreateProjectileMessage(NAPALM, this);
		MessageSystem::GetInstance()->SendMsg(msg);
		msg = nullptr;
		emitter->Burst();
	}

	specialReady = false;
	cooldownTimer = 0;
}

void FlamethrowerGun::Render()
{
	SetX(player->GetX() + player->GetWidth());
	SetY(player->GetY());

	emitter->SetPosition(GetX(),GetY()+GetHeight()*2);
	if(emitter->IsBursting())
		emitter->Render();

	SetDirection(player->GetDirection());

	RECT image = {};

	switch(GetDirection())
	{
	case 3:
		{
			image.top = 10;
			image.bottom = 16;
			image.left = 4;
			image.right = 10;
			SetWidth(6);
			SetHeight(6);
			SetX(player->GetX() + 50);
			SetY(player->GetY() + 20);
			
		}
		break;

	case 0:
		{
			image.top = 14;
			image.bottom = 28;
			image.left = 28;
			image.right = 58;
			SetWidth(30);
			SetHeight(14);
			SetX(player->GetX() + 25);
			SetY(player->GetY() + 10);
		}
		break;

	case 2:
		{
			image.top = 1;
			image.bottom = 7;
			image.left = 4;
			image.right = 10;
			SetWidth(6);
			SetHeight(6);
			SetX(player->GetX() + 25);
			SetY(player->GetY() + 20);
		}
		break;

	/*case 1:
		{
			image.top = 0;
			image.bottom = 14;
			image.left = 28;
			image.right = 58;
			SetWidth(30);
			SetHeight(14);
			SetX(player->GetX() + 25);
			SetY(player->GetY() + 10);
		}
		break;*/
	}

	if(player->GetCurWeapon() == 2)
	{
		if(GetTextureID() != -1)
		{
			CSGD_TextureManager::GetInstance()->Draw(GetTextureID(),
			int(GetX() - GamePlayState::GetInstance()->GetCamX() - player->GetWidth()/2),
			int(GetY() - GamePlayState::GetInstance()->GetCamY() + player->GetHeight()/4), 1.0f, 1.0f, &image);

			/*if(GetDirection() == 3)
			{
				CSGD_TextureManager::GetInstance()->Draw(GetTextureID(),
				int(GetX() + player->GetWidth() - GamePlayState::GetInstance()->GetCamX() - player->GetWidth()),
				int(GetY() - GamePlayState::GetInstance()->GetCamY() + player->GetHeight()/4), 1.0f, 1.0f, &image);
			}
			if(GetDirection() == 2)
			{
				CSGD_TextureManager::GetInstance()->Draw(GetTextureID(),
				int(GetX() + player->GetWidth() + 7 - GamePlayState::GetInstance()->GetCamX() - player->GetWidth()),
				int(GetY() - GamePlayState::GetInstance()->GetCamY() + player->GetHeight()/4), 1.0f, 1.0f, &image);
			}*/
		}
	}
}
